home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / unix / mp14tar.z / mp14tar / mpack / amigados.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  7KB  |  245 lines

  1. /* (c) Copyright 1993 by Mike W. Meyer
  2.  *
  3.  * Permission to use, copy, modify, distribute, and sell this software
  4.  * and its documentation for any purpose is hereby granted without
  5.  * fee, provided that the above copyright notice appear in all copies
  6.  * and that both that copyright notice and this permission notice
  7.  * appear in supporting documentation, and that the name of Mike W.
  8.  * Meyer not be used in advertising or publicity pertaining to
  9.  * distribution of the software without specific, written prior
  10.  * permission.  Mike W. Meyer makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as
  12.  * is" without express or implied warranty.
  13.  *
  14.  * MIKE W. MEYER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16.  * FITNESS, IN NO EVENT SHALL MIKE W. MEYER BE LIABLE FOR ANY SPECIAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  20.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. #include <exec/types.h>
  24. #include <exec/exec.h>
  25. #include <dos/dos.h>
  26.  
  27. #include <libraries/netsupport.h>
  28.  
  29. #ifdef __SASC
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <proto/netsupport.h>
  33. #else
  34. #include <clib/exec_protos.h>
  35. #include <clib/dos_protos.h>
  36. #include <clib/netsupport_protos.h>
  37. #endif
  38.  
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <time.h>
  42. #include <string.h>
  43. #include <dos.h>
  44. #include <ctype.h>
  45. #include <errno.h>
  46.  
  47. #include "common.h"
  48. #include "amiga_protos.h"
  49.  
  50. #define BADCHARS        "#?()|[]%<>:;$&*\\\ \t\`\""
  51. #define FILELEN         29              /* Max file name length */
  52.  
  53. int overwrite_files = 0 ;
  54. static char *output_fname = NULL;
  55. extern int errno, _OSERR ;
  56. void os_perror(char *) ;
  57. char *myGetConfig(char *, char *) ;
  58. int __buffsize = 8192 ;
  59. int didchat;
  60.  
  61. /* Generate a message-id */
  62. char *
  63. os_genid(void) {
  64.         static struct Task *task = NULL ;
  65.         static time_t now ;
  66.         static char hostname[32], domainname[32] ;
  67.         char *result ;
  68.  
  69.         if (task == NULL) {
  70.                 task = FindTask(NULL) ;
  71.                 time(&now) ;
  72.  
  73.                 strcpy(hostname, myGetConfig(NODENAME, "random-amiga"));
  74.                 if (strchr(hostname, '.')) domainname[0] = '\0' ;
  75.                 else strcpy(domainname, myGetConfig(DOMAINNAME, ".random-domain"));
  76.                 if (domainname[0] && domainname[0] != '.') strcat(hostname, ".") ;
  77.                 }
  78.         result = malloc(25 + strlen(hostname) + strlen(domainname)) ;
  79.         sprintf(result, "%d.%d@%s%s", (long) task, now++, hostname, domainname) ;
  80.         return result ;
  81.         }
  82.  
  83. /* Create and return a directory for a message-id */
  84. char *
  85. os_idtodir(char *id) {
  86. static char buf[512] ;
  87.         char *p, save ;
  88.         BPTR *dir ;
  89.  
  90.         strcpy(buf, myGetConfig("METAMAIL_P_DIR", "T:"));
  91.  
  92.         if (!(p = myGetConfig("USERNAME", NULL)))
  93.                 p = myGetConfig("USER", "anonymous");
  94.         AddPart(buf, p, sizeof(buf)) ;
  95.  
  96.         if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  97.                 os_perror(buf) ;
  98.                 return NULL ;
  99.                 }
  100.  
  101.         p = buf + strlen(buf) ;
  102.         *p++ = '/' ;
  103.         save = id[FILELEN] ;
  104.         id[FILELEN] = '\0' ;
  105.         while (*id) {
  106.                 if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id ;
  107.                 id += 1 ;
  108.                 }
  109.         *p  = '\0' ;
  110.         id[FILELEN] = save ;
  111.  
  112.         if (mkdir(buf) == -1 && errno != EEXIST && _OSERR != ERROR_OBJECT_EXISTS) {
  113.                 os_perror(buf) ;
  114.                 return NULL ;
  115.                 }
  116.         strcpy(p, "/") ;
  117.         return buf ;
  118.         }
  119.  
  120. /* Delete the directory created by os_idtodir() */
  121. void
  122. os_donewithdir(char *dir) {
  123.  
  124.         /* Chop off trailing slash */
  125.         dir[strlen(dir) - 1] = '\0' ;
  126.         rmdir(dir) ;
  127.         }
  128.  
  129. /* Create a new file of name "fname". Clean up the name first */
  130. FILE *
  131. os_newtypedfile(char *fname, char *contentType, int binary) {
  132.         char *p, *name, *description, buf[512] ;
  133.         FILE *outfile ;
  134.         static int filesuffix = 0 ;
  135.  
  136.         name = FilePart(fname) ;
  137.  
  138.         /* No BADCHARS */
  139.         for (p = name; *p; p += 1)
  140.                 if (!isprint(*p) || strchr(BADCHARS, *p)) *p = 'X' ;
  141.  
  142.         /* Add a count if we don't have a name, or we aren't overwriting */
  143.         if (!*fname || !overwrite_files) {
  144.                 if (*name) strcpy(buf, name) ;
  145.                 else {
  146.                         name = "part" ;
  147.                         sprintf(buf, "part.%d", ++filesuffix) ;
  148.                         }
  149.                 while (outfile = fopen(buf, "r")) {
  150.                         if (outfile) fclose(outfile) ;
  151.                         sprintf(buf, "%s.%d", name, ++filesuffix) ;
  152.                         }
  153.                 name = buf ;
  154.                 fclose(outfile) ;
  155.                 }
  156.  
  157.         if (!(outfile = fopen(name, "w"))) os_perror(name) ;
  158.  
  159.         if (output_fname) free(output_fname) ;
  160.         output_fname = strsave(name) ;
  161.         description = xmalloc(strlen(name) + 6) ;
  162.         strcpy(description, name) ;
  163.         strcat(description, ".desc") ;
  164.         (void) rename(TEMPFILENAME, description) ;
  165.         free(description) ;
  166.  
  167.         fprintf(stdout, "%s (%s)\n", output_fname, contentType) ;
  168.         didchat = 1;
  169.         return outfile ;
  170.         }
  171.  
  172. /* Warn user that MD5 digest didn't match */
  173. void
  174. os_warnMD5mismatch(void) {
  175.         char *warning;
  176.  
  177.         warning = xmalloc(strlen(output_fname) + 100);
  178.         sprintf(warning, "%s was corrupted in transit", output_fname);
  179.         warn(warning);
  180.         free(warning);
  181.         }
  182.  
  183. /* Report an error (in errno) concerning a filename */
  184. void
  185. os_perror(char *file) {
  186.         if (errno != EOSERR) perror(file);
  187.         else poserr(file) ;
  188.         }
  189.  
  190. /*
  191.  * This getenv will use the WB2.0 calls if you have the 2.0
  192.  * rom. If not, it resorts to looking in the ENV: directory.
  193.  */
  194.  
  195. char *
  196. getenv (const char *name) {
  197.         FILE *fp;
  198.         char *ptr;
  199.         static char value[256] ;
  200.         static char buf[256] ;
  201.  
  202.         /* 2.0 style? */
  203.         if (DOSBase->dl_lib.lib_Version >= 36) {
  204.                 if (GetVar ((char *)name,value,256,0L) == -1) return NULL;
  205.         } else {
  206.                 if (strlen (name) > 252) return NULL;
  207.                 strcpy (buf,"ENV:");
  208.                 strcpy (&buf[4],name);
  209.                 if (! (fp = fopen(buf,"r"))) return NULL;
  210.                 for (ptr = value; (*ptr=getc(fp))!=EOF
  211.                         && *ptr != '\n'
  212.                         && ++ptr < &value[256];);
  213.                 fclose(fp);
  214.                 *ptr = 0;
  215.                 }
  216.         return value;
  217.         }
  218.  
  219. /* Get configuarion, either from a file or from a variable */
  220.  
  221. char * myGetConfig(char *keyword, char *def) {
  222.         char *entry;
  223.  
  224.         if (NetSupportBase)
  225.                 return GetConfig(NULL, keyword, NULL, def);
  226.  
  227.  
  228.         entry = getenv(keyword);  /* Library is NOT available */
  229.         return ((entry) ? entry : def);
  230.         }
  231.  
  232. /* (Don't) Handle a BinHex'ed file */
  233.  
  234. int
  235. os_binhex(FILE *infile, int dummy1, int dummy2) {
  236.         return 1;
  237.         }
  238. /*
  239.  * Close a file opened by os_newTypedFile()
  240.  */
  241. int
  242. os_closetypedfile(FILE *outfile) {
  243.         return fclose(outfile);
  244.         }
  245.